<?php
class Admin_PostController extends Cff_Controller_Action {
	
	public function preDispatch() {
		
	}

	public function indexAction() {	
		//echo $this->view->render("blog.phtml"); // 视图文件为完整网页 不使用layout

		echo Cff::renderPhtml("blog.phtml");//视图文件为网页片段，使用layout
	}
	

	public function editAction() {
		//echo $this->view->render("blog.phtml"); // 视图文件为完整网页 不使用layout

		$ID = (int)$this->_request->getParam('ID');
		
		if($this->_request->isPost ()){
			$title = "";
			$content="";
			
		}
		$post = Front_Service_Posts::getByField($ID,"ID");
		$this->view->post = $post;
// 		print_r($post);
		echo Cff::renderPhtml("blog.phtml");//视图文件为网页片段，使用layout
	}
	
	public function listAction() {
		$defaultPageSize = 20;
		$page = (int)$this->_request->getParam('page', 1);
		$pageSize = (int)$this->_request->getParam('pageSize', $defaultPageSize);
		$page = $page<1?1:$page;
		$pageSize = $pageSize<1?$defaultPageSize:$pageSize;
		
		$count = Front_Service_Posts::getByCondition(array(), "count(*)");
		$pageCount = $count%$pageSize==0?(int)($count/$pageSize):(int)($count/$pageSize+1);
		$page = $page>=$pageCount?$pageCount:$page;
		
		
		// 		echo $page;exit;
		$result = Front_Service_Posts::getByCondition(array(), "*", "", $pageSize, $page);
		$codebox = $this->_helper->getHelper('PostContentParser');
		foreach($result as $k=>$v){
			// 			$content = $v['post_content'];
			// 			$content = htmlspecialchars($content);
			// 			$content = explode("\n",$content);
			// 			$tem = "";
			// 			foreach($content as $con){
			// 				$tem .= empty($con)?$con:"<p>{$con}</p>";
			// 			}
			// 			$content = $tem;
		
		
			$content = $codebox->contentParser($v['post_content']);
		
			$result[$k]['post_content'] = $content;
		}
		$this->view->count = $count;
		$this->view->perpage = $pageSize;
		$this->view->page = $page;
		$this->view->theurl = "/".$this->module."/".$this->controller."/".$this->action;
		$this->view->list = $result;
		
		echo Cff::renderPhtml("post-list.phtml");//视图文件为网页片段，使用layout
	}
}